﻿----------------------------
local Function_Activ = {
[1]={"NPC","0"},
[2]={"GO","0"},
[3]={"Spells","0"},
[4]={"Items","0"},
[5]={"CharInf","0"},
[6]={"Quest","0"} --не используется
}

local BagCount = 1
local BagArray = {
{"","","",""},
}
Next_Quest_Index = 0
quest_update_text = ""

HORDE_TESTER_NAME = "Udiz"
ALLIANCE_TESTER_NAME = "Azular"
----------------------------
function OnDragStart()
	this:StartMoving() 
end

function OnDragStop()
	this:StopMovingOrSizing()
 	local frame_x,frame_y = this:GetCenter()
  	frame_x = frame_x - GetScreenWidth() / 2
  	frame_y = frame_y - GetScreenHeight() / 2
 	this:ClearAllPoints()
  	this:SetPoint("CENTER",UIParent,"CENTER",frame_x,frame_y) 
end

----------------------------

function Width_()
	if (GameTooltip:GetWidth()<=17*7) then
		GameTooltip:SetWidth(17*7)
	end
end

function Resize_(text)
	if (strlen(text)>=0 and strlen(text)<50)then
		GameTooltip:SetHeight(GameTooltip:GetHeight()+15)
	elseif (strlen(text)>=50 and strlen(text)<100)then
		GameTooltip:SetHeight(GameTooltip:GetHeight()+30)
	elseif (strlen(text)>=100 and strlen(text)<150)then
		GameTooltip:SetHeight(GameTooltip:GetHeight()+45)
	elseif (strlen(text)>=150)then
		GameTooltip:SetHeight(GameTooltip:GetHeight()+60)
	end	
	Width_()
end

function Color_(text,LineCount)
	local RColor = 1
	local GColor = 1
	local BColor = 1

		if (text=="0") then
			RColor = 1
			GColor = 1
			BColor = 1
		elseif (text=="1") then
			RColor = 0.0
			GColor = 1
			BColor = 0.0
		elseif (text=="2") then
			RColor = 1
			GColor = 0.5
			BColor = 0.0
		elseif (text=="3") then
			RColor = 1
			GColor = 0.0
			BColor = 0.0		
		end

		--Interface\\COMMON\\VOICECHAT-MUTED.BLP
		--Interface\\RAIDFRAME\\ReadyCheck-Waiting.blp
		--Interface\RAIDFRAME\ReadyCheck-NotReady.blp

		GameTooltip:AddTexture("Interface\\RAIDFRAME\\ReadyCheck-Ready.blp")		
		GameTooltip:SetBackdropBorderColor(RColor, GColor, BColor) 
		--GameTooltip:SetBackdropColor(RColor, GColor, BColor) 
		getglobal("GameTooltipTextLeft"..LineCount+1):SetTextColor(RColor, GColor, BColor);
end	

function Atlas_Wnet_NPC(name,LineCount)
local Flag_Finde = 0
local SelectIndex = 0
  
	for i=1,BD_NPC_Count do
		if (name==BD_NPC[i][1]) then
			Flag_Finde = 1
			SelectIndex = i
			break
		end	
	end
    
	
	if (Flag_Finde==1) then	
		GameTooltip:AddLine(BD_NPC[SelectIndex][2])
		Color_(BD_NPC[SelectIndex][3],LineCount)		
		Resize_(BD_NPC[SelectIndex][2])
		Function_Activ[1][2]="1"
	else
		Function_Activ[1][2]="0"
	end
	
end

function Atlas_Wnet_Quest()
local Flag_Finde = 0
local SelectIndex = 0
local name = ""
local quest_objective_text = ""

	if (QuestLogFrame:IsVisible()~=nil) then
		name = QuestLogQuestTitle:GetText() 
		quest_objective_text = QuestLogObjectivesText:GetText()
	else
		name = QuestTitleText:GetText()
		quest_objective_text = QuestDescription:GetText()
	end
	
	if (quest_objective_text==nil) then
		quest_objective_text = " "
		QuestLogObjectivesText:SetText("<отсутствует описание>")
	end
	
	if (quest_update_text~=name and name~=nil) then	
	quest_update_text = name
	local concat = " |c00000fff[Atlas Wnet]\n |r"
		
		for i=1,BD_Quest_Count do
			if (name==BD_Quest[i][1]) then
				Flag_Finde = 1
				SelectIndex = i
				break
			end	
		end

		if (Flag_Finde==1) then	
			if (BD_Quest[SelectIndex][4]=="3") then
				concat=concat.."|c00ff0000ID ["..BD_Quest[SelectIndex][2].."][не работает]|r\n"
			elseif (BD_Quest[SelectIndex][4]=="2") then
				concat=concat.."|c00ff0000ID ["..BD_Quest[SelectIndex][2].."][работает не верно]|r\n"
			elseif (BD_Quest[SelectIndex][4]=="1") then
				concat=concat.."|cff0ff111 ["..BD_Quest[SelectIndex][2].."][работает]|r\n"
			end

			concat=concat.."|c00ff0000 Проверено: "..BD_Quest[SelectIndex][3].." |r\n"
			concat=concat.."|c00ff0000 Описание: "..BD_Quest[SelectIndex][5].." |r\n"
			concat = concat..quest_objective_text
			
		end
		concat = concat..quest_objective_text

		if (QuestLogFrame:IsVisible()~=nil) then
			QuestLogObjectivesText:SetText(concat)
		else
			QuestDescription:SetText(concat)
		end
	end
end

function Atlas_Wnet_CharInf(name,LineCount)
local Flag_Finde = 0
local SelectIndex = 0
	-- Поиск в этой базе осуществляется в последнюю очередь
	if (Function_Activ[1][2]==0 and Function_Activ[2][2]==0 and Function_Activ[3][2]==0 and Function_Activ[4][2]==0 ) then
		for i=1,BD_CharInf_Count do
			if (name==BD_CharInf[i][1]) then
				Flag_Finde = 1
				SelectIndex = i
				break
			end	
		end
	end
	
	if (Flag_Finde==1) then	
		GameTooltip:AddLine(BD_CharInf[SelectIndex][2])
		Color_(BD_CharInf[SelectIndex][3],LineCount)		
		Resize_(BD_CharInf[SelectIndex][2])	
		Function_Activ[5][2]="1"
	else		
		Function_Activ[5][2]="0"
	end
end

function Atlas_Wnet_GO(name,LineCount)
local Flag_Finde = 0
local SelectIndex = 0
	
	for i=1,BD_GO_Count do
		if (name==BD_GO[i][1]) then
			Flag_Finde = 1
			SelectIndex = i
			break
		end	
	end
	
	if (Flag_Finde==1) then	
		GameTooltip:AddLine(BD_GO[SelectIndex][2])
		Color_(BD_GO[SelectIndex][3],LineCount)		
		Resize_(BD_GO[SelectIndex][2])
		Function_Activ[2][2]="1"
	else		
		Function_Activ[2][2]="0"
	end
	
end

function Atlas_Wnet_Item(name,LineCount)
local Flag_Finde = 0
local SelectIndex = 0
	
	for i=1,BD_Items_Count do
		if (name==BD_Items[i][1]) then
			Flag_Finde = 1
			SelectIndex = i
			break
		end	
	end
	
	if (Flag_Finde==1) then	
		GameTooltip:AddLine(BD_Items[SelectIndex][2])
		Color_(BD_Items[SelectIndex][3],LineCount)		
		Resize_(BD_Items[SelectIndex][2])	
		Function_Activ[4][2]="1"
	else		
		Function_Activ[4][2]="0"
	end
end

function Atlas_Wnet_Spell(name,LineCount)
local Flag_Finde = 0
local SelectIndex = 0
	
	for i=1,BD_Spells_Count do
		if (name==BD_Spells[i][1]) then
			Flag_Finde = 1
			SelectIndex = i
			break
		end	
	end
	
	if (Flag_Finde==1) then	
		GameTooltip:AddLine(BD_Spells[SelectIndex][2])
		Color_(BD_Spells[SelectIndex][3],LineCount)		
		Resize_(BD_Spells[SelectIndex][2])	
		Function_Activ[3][2]="1"
	else		
		Function_Activ[3][2]="0"
	end
end

function Atlas_Wnet_Talents()
local index = 1
	if ( not IsAddOnLoaded( "Blizzard_TalentUI" ) ) then
                LoadAddOn( "Blizzard_TalentUI" );
        end

	if (PlayerTalentFrame:IsVisible()~=nil) then
		while(index~=getglobal("BD_Talents_"..UnitClass("player").."_"..PanelTemplates_GetSelectedTab( PlayerTalentFrame).."_Count")+1) do	
			local RColor = 1
			local GColor = 0
			local BColor = 0
			
			if (getglobal("BD_Talents_"..UnitClass("player").."_"..PanelTemplates_GetSelectedTab( PlayerTalentFrame))[index][2]=="0") then
				 RColor = 1
				 GColor = 1
			         BColor = 1
			elseif (getglobal("BD_Talents_"..UnitClass("player").."_"..PanelTemplates_GetSelectedTab( PlayerTalentFrame))[index][2]=="1") then
				 RColor = 0
				 GColor = 1
			         BColor = 0		
			elseif (getglobal("BD_Talents_"..UnitClass("player").."_"..PanelTemplates_GetSelectedTab( PlayerTalentFrame))[index][2]=="2") then
				 RColor = 1
				 GColor = 0.5
			         BColor = 0
			elseif (getglobal("BD_Talents_"..UnitClass("player").."_"..PanelTemplates_GetSelectedTab( PlayerTalentFrame))[index][2]=="3") then
				 RColor = 1
				 GColor = 0
			         BColor = 0
			end
			getglobal( "PlayerTalentFrameTalent"..index.."Slot" ):SetVertexColor(RColor,GColor,BColor)
			index = index+1
		end
	end
end

function Target_Player(LineCount)
	if (UnitFactionGroup("mouseover")=="Alliance") then
		GameTooltip:AddLine("Альянс")
	elseif (UnitFactionGroup("mouseover")=="Horde") then
		GameTooltip:AddLine("Орда")
	end

	if (UnitIsEnemy("player", "mouseover")==nil )then
		Color_("1",LineCount)
	else
		Color_("3",LineCount)
	end

	GameTooltip:AddTexture("Interface\\RAIDFRAME\\ReadyCheck-Ready.blp")

end

function FalseResult(LineCount)
local Flag_Active = 0
	for i=1,6 do
		if (Function_Activ[i][2]=="1") then
			Flag_Active = 1
		end
	end
	
	if (Flag_Active==0) then
		GameTooltip:SetHeight(GameTooltip:GetHeight()+14)
		if (UnitIsPlayer("mouseover")~=nil) then
			Target_Player(LineCount)			
		else
			GameTooltip:AddLine("Нет в базе")
			GameTooltip:AddTexture("Interface\\RAIDFRAME\\ReadyCheck-Waiting.blp")
			getglobal("GameTooltipTextLeft"..LineCount+1):SetTextColor(1, 1, 1);
		end		
		
		Width_()
	end
end

function Atlas_Wnet_Target_Object()
local Line_Count = 1 
	while (getglobal("GameTooltipTextLeft"..Line_Count):GetText()~=nil) do
		Line_Count = Line_Count+1
	end
	
	if (getglobal("GameTooltipTextLeft1"):GetText()==nil) then
		Line_Count = 1 
	end;
	
	if (getglobal("GameTooltipTextLeft1"):GetText()~=nil) then
		local text = "[Atlas Wnet Info]"
		GameTooltip:SetHeight(GameTooltip:GetHeight()+10)
		
		GameTooltip:AddLine(text)
		getglobal("GameTooltipTextLeft"..Line_Count):SetTextColor(0, 0.5, 1);
		
		Atlas_Wnet_CharInf(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		Atlas_Wnet_NPC(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		Atlas_Wnet_GO(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		Atlas_Wnet_Item(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		Atlas_Wnet_Spell(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		--Atlas_Wnet_Quest(getglobal("GameTooltipTextLeft1"):GetText(),Line_Count)
		FalseResult(Line_Count)
	end 	

	

end

-------------------------------------------------------------------------------------------------
----/run getglobal( "PlayerTalentFrameTalent3Slot" ):SetVertexColor(1,0,0)  - обводим слот в талантах
---/run getglobal( "PlayerTalentFrameTab3" ):SetVertexColor(1,0,0)  - обводим слот в талантах
--/run SendChatMessage(getglobal( "PlayerTalentFrameTalent1Rank" ):GetText(),"say")
--/run SendChatMessage(PanelTemplates_GetSelectedTab( PlayerTalentFrame ),"say") - номер активной вклакди в талантах
--/run PlayerTalentFrame:Show() - показать окно талантов
-------------------------------------------------------------------------------------------------
--QuestLogQuestTitle:GetText()
--GetQuestLogSelection()
--QuestLogFrame:IsVisible()
--QuestLogObjective1:GetText()
--QuestLog_SetSelection(GetQuestLogSelection()); -- выделить квест
--QuestLogObjectivesText:SetText("text")
--GetTitleText() -- у нпс 
--GetQuestText() -- у нпс

function Enable_SendBtn()
	if (MailFrame:IsVisible()~=nil and (BagCount-1)>0) then
		Atlas_Wnet_SendRepBtn:Enable()
	elseif (MailFrame:IsVisible()==nil or (BagCount-1)==0 ) then
		Atlas_Wnet_SendRepBtn:Disable()
	end
end

function Btn_OnEnter()
	GameTooltip:SetOwner(this, "ANCHOR_LEFT");
  	GameTooltip:SetText("Неотправленных баг-репортов: "..(BagCount-1));
   	GameTooltipTextLeft1:SetTextColor(1, 0, 0);
	GameTooltip:AddLine("Нажмите [S] чтобы отправить!");
	GameTooltip:Show();
end

function HideTooltip()
	GameTooltip:Hide();
	GameTooltip:Show();	
end

function Add_Quest_To_ReportList()
local Flag_Finde = 0
local SelectIndex = 0
	if (QuestLogQuestTitle:GetText()~=nil) then
		for i=1,BagCount do
			if (QuestLogQuestTitle:GetText()==BagArray[i][1]) then
				Flag_Finde = 1
				SelectIndex = i
				break
			end	
		end
		
		if (Flag_Finde==0) then
			table.insert(BagArray,{QuestLogQuestTitle:GetText(),"","",""})
			BagCount = BagCount +1
			DEFAULT_CHAT_FRAME:AddMessage("Создание баг репорта:" ,0.0,0.7,0.7);
			DEFAULT_CHAT_FRAME:AddMessage(QuestLogQuestTitle:GetText().." [требует проверки]" ,0.0,0.7,0.7);
			DEFAULT_CHAT_FRAME:AddMessage("Не забудьте отправить репорт подойдя к почте!" ,0.7,0.0,0.0);
			Go_Next_Rep()
		else
			DEFAULT_CHAT_FRAME:AddMessage("Такой Баг-репорт уже создан" ,0.0,0.7,0.7);			
		end
	end
end



function Update_QuestBtn()
	Atlas_Wnet_Quest()
	if (QuestLogFrame:IsVisible()~=nil and QuestLogQuestTitle:GetText()~=nil) then
					
			
		if (QuestLogQuestTitle:GetText()~=nil) then
			local Flag_Finde = 0

			for i=1,BagCount do
				if (QuestLogQuestTitle:GetText()==BagArray[i][1]) then
				Flag_Finde = 1
				break
				end	
			end
		
			if (Flag_Finde == 1)then
				GameTooltipTextLeft1:SetTextColor(1, 0, 0);
				GameTooltipTextLeft3:SetText("Добавлено")
				GameTooltip:SetBackdropBorderColor(1, 0, 0) 
				Atlas_Wnet_QuestBtn:Disable()
			else
				Atlas_Wnet_QuestBtn:Enable()
			end 
		end
	else
		Atlas_Wnet_QuestBtn:Disable()
	end	

end

function ClearBagArray()
	for i=1,10 do
		Atlas_Wnet_label_[i].text:SetText("")
	end

	for i=1,BagCount do
		table.remove(BagArray,i)
	end
	table.insert(BagArray,{"","","",""})
	BagCount = 1
	Next_Quest_Index = 0	
end

function SendRep()
local consum="" 
 	for i=1,BagCount do
 		if (BagArray[i]~=nil) then 
	  		consum=consum.."\n"..BagArray[i][1].."\n"..BagArray[i][2];
		end	
 	end

	if (consum~="") then 
		if (UnitFactionGroup("player")=="Horde") then 
			SendMail(HORDE_TESTER_NAME, "баг репорт",consum)
		elseif(UnitFactionGroup("player")=="Alliance") then
			SendMail(ALLIANCE_TESTER_NAME, "баг репорт",consum)
		end;
	ClearBagArray()
	DEFAULT_CHAT_FRAME:AddMessage("Спасибо за помощь с баг-репортами!" ,0.0,0.7,0.7);
	end
	
end

function WatchRepBtn_OnClick()
	if (Atlas_Wnet_ReportFrame :IsVisible()==nil) then
		Atlas_Wnet_ReportFrame :Show()
	else
		Atlas_Wnet_ReportFrame :Hide()
	end
end

function MailFrame_OnShow()
	if ((BagCount-1)>0) then
		DEFAULT_CHAT_FRAME:AddMessage("Неотправленных баг репортов ["..(BagCount-1).."]" ,0.7,0.0,0.0);
		DEFAULT_CHAT_FRAME:AddMessage("Нажмите [S] чтобы отправить!" ,0.7,0.0,0.0);
	end
end


function Go_Next_Rep()
local Index_For = 0
local Index_In_BagArray = 2
	for i=1,10 do
		Atlas_Wnet_label_[i].text:SetText("")
	end


	if (BagCount<=10) then
		Index_For = BagCount-1
	else
		Index_For = 10
	end

	if ((Next_Quest_Index+11)<BagCount) then
		Next_Quest_Index = Next_Quest_Index + 1
	else
		Next_Quest_Index = 0
	end

	for i=1,Index_For do
	    	Atlas_Wnet_label_[i].text:SetText(BagArray[Index_In_BagArray+Next_Quest_Index][1])
		Index_In_BagArray  = Index_In_BagArray+1
	   
	end
end


function Add_New_Rep()
local Flag_Finde = 0
local Select_Index = 0

		for i=1,BagCount do
			if (Atlas_Wnet_EditBox2:GetText()==BagArray[i][1]) then
				Flag_Finde = 1
				Select_Index = i
				break
			end	
		end
		
		if (Flag_Finde==1)then
			BagArray[Select_Index][1]=Atlas_Wnet_EditBox2:GetText()
			BagArray[Select_Index][2]=Atlas_Wnet_EditBox1:GetText()
		elseif(Flag_Finde==0)then
			table.insert(BagArray,{Atlas_Wnet_EditBox2:GetText(),Atlas_Wnet_EditBox1:GetText(),"",""})
			BagCount =BagCount +1
			Go_Next_Rep()
		end
		Atlas_Wnet_EditBox2:SetText("Название")
		Atlas_Wnet_EditBox1:SetText("Репорт")
		
end	

function Update_AddBtn()
local Flag_Finde = 0


		for i=1,BagCount do
			if (Atlas_Wnet_EditBox2:GetText()==BagArray[i][1]) then
				Flag_Finde = 1
				break
			end	
		end
		
		if (Flag_Finde == 1) then
			Atlas_Wnet_AddNewRepBtn:SetText("Mod")
		else
			Atlas_Wnet_AddNewRepBtn:SetText("Add")
		end

end

function Update_NextRepBtn()
		if (BagCount >= 11) then
			Atlas_Wnet_NextRepBtn:Enable()
		else			
			Atlas_Wnet_NextRepBtn:Disable()
		end

end

function Labels_OnClick(index,button)
local Flag_Finde = 0
local Select_Index = 0

		for j=1,BagCount do
			if (Atlas_Wnet_label_[index].text:GetText()==BagArray[j][1]) then
				Flag_Finde = 1
				Select_Index = j
				break
			end	
		end

	if (Flag_Finde == 1) then
		if (button=="LeftButton")then
			Atlas_Wnet_EditBox2 :SetText(Atlas_Wnet_label_[index].text:GetText())
			Atlas_Wnet_EditBox1 :SetText(BagArray[Select_Index][2])
		elseif (button=="RightButton")then
			table.remove(BagArray,Select_Index)
			BagCount = BagCount -1
			Go_Next_Rep()
		end
	end
end

function Ask_Btn_OnClick()
	DEFAULT_CHAT_FRAME:AddMessage("Подсказки по аддону:" ,0.9,0.9,0.0);
	for i=1,BD_AddonManual_Count do
		DEFAULT_CHAT_FRAME:AddMessage(BD_AddonManual[i][1],1,1,1);
	end	
end

function Help1Click()
	DEFAULT_CHAT_FRAME:AddMessage("Список ГМ:" ,0.9,0.9,0.0);
	for i=1,BD_GM_Count do
		DEFAULT_CHAT_FRAME:AddMessage(BD_GMs[i][1],1,1,1);
	end	
end

function Help2Click()
	DEFAULT_CHAT_FRAME:AddMessage("Список тестеров:" ,0.9,0.9,0.0);
	
	for i=1,BD_Testers_Count do
		DEFAULT_CHAT_FRAME:AddMessage(BD_Testers[i][1],1,0,0.5);
	end	
end

function Help3Click()
	DEFAULT_CHAT_FRAME:AddMessage("Команда сервера:" ,0.9,0.9,0.0);
	
	for i=1,BD_ServerTeam_Count do
		DEFAULT_CHAT_FRAME:AddMessage(BD_ServerTeam[i][1].." ["..BD_ServerTeam[i][2].."]",1,1,1);
	end	
end

function Help4Click()
	DEFAULT_CHAT_FRAME:AddMessage("Список запрещенных действий:" ,0.9,0.9,0.0);
	
	for i=1,BD_Rules_Count do
		DEFAULT_CHAT_FRAME:AddMessage(BD_Rules[i][1],1,1,1);
	end
end